/*
 * Copyright [2009] [Marcin Rzeźnicki]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
package mr.go.yaxc.actions;

import java.util.HashMap;

import mr.go.yaxc.Action;
import mr.go.yaxc.ConfigurationError;
import mr.go.yaxc.ScopeHandler;

import org.xml.sax.Attributes;

/**
 * This action creates a new instance of <i>class</i> using non-default
 * constructor. Used with {@link Constructor}
 * 
 * <h6>on xml element start:</h6> Sets <i>property</i> 'class' with a value of
 * <i>class</i> attribute <h6>on xml element end:</h6> tries to obtain
 * <i>property</i> 'object' from local scope then passes it to parent's
 * <i>params</i>, or, if <i>id</i> attribute is set, puts it in the global
 * context <h6>prerequisites:</h6> none <h6>attributes:</h6>
 * <table>
 * <tr>
 * <th>Attribute name</th>
 * <th>What is it for?</th>
 * <th>Required?</th>
 * </tr>
 * <tr>
 * <td><i>class</i></td>
 * <td>qualified classname of instance to be created</td>
 * <td>yes</td>
 * </tr>
 * <tr>
 * <td><i>id</i></td>
 * <td>if set, puts new instance in global context</td>
 * <td>no</td>
 * </tr>
 * </table>
 *<p>
 * Example:<br>
 * 
 * <pre>
 * &lt;Create class=&quot;mr.go.yaxc.tests.test2.Something&quot;&gt;
 * 				&lt;Constructor&gt;
 * 					&lt;String&gt;a&lt;/String&gt;
 * 					&lt;Integer&gt;2&lt;/Integer&gt;
 * 				&lt;/Constructor&gt;
 * 			&lt;/Create&gt;
 * </pre>
 * 
 * </p>
 * 
 * 
 * @author Marcin Rzeźnicki
 * 
 */
public class Create extends Action {

    private String id;

    @Override
    public void carryOut(String name, Attributes attributes,
                         ScopeHandler handler)
            throws Exception {
        String classname = attributes.getValue(getAttribute("class"));
        if (classname == null) {
            throw new ConfigurationError("Missing attribute: " + getAttribute(
                    "class"));
        }
        id = attributes.getValue("id");
        properties = new HashMap<String, Object>();
        properties.put("class",
                       classname);
    }

    @Override
    public void processed(String body, ScopeHandler handler)
            throws Exception {
        final Object o = properties.get("object");
        if (o != null) {
            if (id != null) {
                handler.setReference(id,
                                     object);
                if (handler.getLogger().isDebugEnabled()) {
                    handler.getLogger().debug("Id '{}': {}",
                                              id,
                                              object.toString());
                }
            }
            handler.tryAddParam(o);
        }
    }
}
